package std

import (
	"database/sql"
	"fmt"
	"net/url"

	"rdbmsx/config"

	"github.com/ClickHouse/clickhouse-go/v2"
)

func MultiStdHost() error {
	env := config.ClickHouseConfig.Option(func(options *clickhouse.Options) {
		options.ConnOpenStrategy = clickhouse.ConnOpenRoundRobin
	})

	conn, err := clickhouse.Open(&env)
	if err != nil {
		return err
	}

	v, err := conn.ServerVersion()
	if err != nil {
		return err
	}

	fmt.Println(v.String())

	return nil
}

func MultiStdHostDSN() error {
	values := url.Values{}
	values.Set("connection_open_strategy", "round_robin")
	env := config.ClickHouseConfig.DSN(values)
	//conn, err := sql.Open("clickhouse", fmt.Sprintf("clickhouse://127.0.0.1:9001,127.0.0.1:9002,%s:%d?username=%s&password=%s&connection_open_strategy=round_robin", env.Host, env.Port, env.Username, env.Password))
	conn, err := sql.Open("clickhouse", env)
	if err != nil {
		return err
	}

	return conn.Ping()
}
